/**
 * Author : wang885298.
 * Date   : 2018/7/20.
 * File   : util.
 */

/***
 * 节流函数 在wait间隔时间内不再进行任何调用时，触发一次最后的调用（如:wait设置为300ms，一直点击，该函数一直不执行，只有在间隔了300ms不做任何点击后，才执行刚才的最后一次点击）
 * @param func 执行函数
 * @param wait 等待时间
 * @param {Boolean} immediate 是否立即执行
 * @return {Function}
 */
function throttle (func, wait, immediate) {
  var timeout
  return function () {
    let context = this
    let args = arguments
    let later = function () {
      timeout = null
      if (!immediate) func.apply(context, args)
    }
    let callNow = immediate && !timeout
    clearTimeout(timeout)
    timeout = setTimeout(later, wait)
    if (callNow) func.apply(context, args)
  }
}

/**
 * 判断对象是否为空对象
 * @param e 判断对象
 * @returns {boolean}
 */
function isEmptyObject (e) {
  // vue 框架给数组添加了一个$remove的方法，所以对数组单独判断
  if (e instanceof Array) {
    if (e.length > 0) {
      return false
    } else {
      return true
    }
  }
  for (var t in e) {
    return false
  }
  return true
}

let MAPSCALEMAP = {
  2: '1000000',
  3: '1000000',
  4: '500000',
  5: '200000',
  6: '100000',
  7: '20000',
  8: '30000',
  9: '20000',
  10: '10000',
  11: '5000',
  12: '2000',
  13: '1000',
  14: '500',
  15: '200',
  16: '100',
  17: '50',
  18: '25',
  19: '10',
  20: '10'
}
let cmp = function (x, y) {
  // If both x and y are null or undefined and exactly the same

  function innerCmp (a, b) {
    if (a === b) {
      return true
    }
    // If they are not strictly equal, they both need to be Objects
    if (!(a instanceof Object) || !(b instanceof Object)) {
      return false
    }
    // They must have the exact same prototype chain,the closest we can do is
    // test the constructor.
    if (a.constructor !== b.constructor) {
      return false
    }
    for (let p in a) {
      // Inherited properties were tested using x.constructor === y.constructor
      if (a.hasOwnProperty(p)) {
        // Allows comparing x[ p ] and y[ p ] when set to undefined
        if (!b.hasOwnProperty(p)) {
          return false
        }
        // If they have the same strict value or identity then they are equal
        if (a[p] === b[p]) {
          continue
        }
        // Numbers, Strings, Functions, Booleans must be strictly equal
        if (typeof (a[p]) !== 'object') {
          return false
        }
        let tmptag = innerCmp(a[p], b[p])
        if (tmptag === false) return false
        continue
        // if (x[p] !== y[p]) return false
        // Objects and Arrays must be tested recursively
      }
    }
    for (let q in b) {
      // allows x[ p ] to be set to undefined
      if (b.hasOwnProperty(q) && !a.hasOwnProperty(q)) {
        return false
      }
    }
  }

  let tmpTag = true
  tmpTag = innerCmp(x, y)
  if (tmpTag === false) return false
  return true
}

function covertToRect (TL, TR, BL, BR) {
  let maxLng = Math.max(TL.lng, TR.lng, BL.lng, BR.lng)
  let maxLat = Math.max(TL.lat, TR.lat, BL.lat, BR.lat)
  let minLng = Math.min(TL.lng, TR.lng, BL.lng, BR.lng)
  let minLat = Math.min(TL.lat, TR.lat, BL.lat, BR.lat)
  return {southwest: {lng: minLng, lat: minLat}, northeast: {lng: maxLng, lat: maxLat}}
}

export {
  covertToRect,
  cmp,
  MAPSCALEMAP,
  throttle,
  isEmptyObject
}
